package com.leo.train.business.controller.admin;

import com.leo.train.business.dto.TrainDto;
import com.leo.train.business.dto.TrainQueryDto;
import com.leo.train.business.entity.Train;
import com.leo.train.business.service.SeatService;
import com.leo.train.business.service.TrainService;
import com.leo.train.business.vo.TrainQueryVo;
import com.leo.train.common.resp.CommonResp;
import com.leo.train.common.resp.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/train")
@Slf4j
public class AdminTrainController {
    @Autowired
    private TrainService trainService;
    @Autowired
    private SeatService seatService;
    /**
     * 新增
     * @param trainDto
     * @return
     */
    @PostMapping("/add")
    public CommonResp save(@RequestBody TrainDto trainDto) {
        trainService.save(trainDto);
        return new CommonResp<>();
    }

    /**
     * 修改
     * @param trainDto
     * @return
     */
    @PostMapping("/update")
    public CommonResp update(@RequestBody TrainDto trainDto) {
        trainService.save(trainDto);
        return new CommonResp<>();
    }

    /**
     * 查询车次列表
     * @param trainQueryDto
     * @return
     */
    @GetMapping("/list")
    public CommonResp<PageResult<TrainQueryVo>> list(TrainQueryDto trainQueryDto) {
        return new CommonResp<>(trainService.list(trainQueryDto));
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @GetMapping("/detail")

    public CommonResp<TrainQueryVo> detail(@RequestParam Long id) {
        return new CommonResp<>(trainService.detail(id));
    }

    /**
     * 删除
     * @param params
     * @return
     */
    @PostMapping("/delete")
    public CommonResp delete(@RequestBody Map<String, Long> params) {
        Long id = params.get("id");
        trainService.delete(id);
        return new CommonResp<>();
    }

    /**
     * 查询车次编码所有列表
     */
    @GetMapping("/listAll")
    public CommonResp<List<Train>> listAll() {
        return new CommonResp<>(trainService.listAll());
    }

    /**
     * 生成座位
     * @param params
     * @return
     */
    @PostMapping("/generateSeats")
    public CommonResp generateSeats(@RequestBody Map<String, String> params) {
        String trainCode = params.get("code");
        seatService.generateSeats(trainCode);
        log.info("生成座位开始，trainCode: {}", trainCode);
        return new CommonResp<>();
    }

}
